#!/usr/bin/env python
# coding=utf8


import sys

sys.path.append('..')
sys.path.append('../../')
from i_util.pybeanstalk import PyBeanstalk
import json
import traceback

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.transport.TTransport import TMemoryBuffer
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
from bdp.i_crawler.i_extractor.ttypes import BaseInfo, CrawlInfo, ExtractInfo, PageParseInfo
from bdp.i_crawler.i_entity_extractor.ttypes import EntityExtractorInfo, EntitySource
import time
from pymongo import MongoClient
import hashlib
from i_util import tools
from copy import copy

reload(sys)
sys.setdefaultencoding("utf-8")


def write_file(f, data_list):
    for item in data_list:
        f.write(item)
        f.write('\n')


def main():
    host = '101.201.100.58'
    port = 27017
    database = 'wenshu_court_gov'
    coll = 'wenshu_content'
    client = MongoClient(host, port)
    db = client[database][coll]
    cursor = db.find()
    num = 0
    sendnum = 0
    begin_time = time.time()
    case_type_map = {"1": u"刑事案件", "2": u"民事案件", "3": u"行政案件", "4": u"赔偿案件", "5": u"执行案件"}
    f = open('no_content_id.txt', 'w')
    ff = open('sended_id.txt', 'w')
    no_content_id_list = []
    sended_id_list = []
    no_content_id_num = 0
    try:
        for item in cursor:
            num += 1
            if num >= 0:

                src_url = item.get('_id', '')

                doc_content = item.get('DocContent', '')
                doc_id = item.get(u'文书ID', '')
                if doc_content == "":
                    no_content_id_num += 1
                    no_content_id_list.append(str(doc_id))
                    if no_content_id_num % 5000 == 0:
                        write_file(f, no_content_id_list)
                    continue

                case_name = item.get(u'案件名称', '')
                case_type = case_type_map.get(item.get(u'案件类型', ''), '')
                procedure = item.get(u'审判程序', '')

                case_date = item.get(u'裁判日期', '')
                court = item.get(u'法院名称', '')
                bulletin_date = item.get('bulletin_date', '')
                case_id = item.get('case_id', '')

                data = {
                    'src_url': src_url,
                    'doc_content': doc_content,
                    'case_name': case_name,
                    'case_type': case_type,
                    'procedure': procedure,
                    'doc_id': doc_id,
                    'bulletin_date': bulletin_date,
                    'case_date': case_date,
                    'case_id': case_id,
                    'court': court,

                }
                data = json.dumps(data)
                site_id = get_md5_i64(item.get("site_id", ""))
                site = src_url.split('//')[-1].split('/')[0]
                base_info = BaseInfo(url=src_url, site_id=site_id, site=site)
                extract_info = ExtractInfo(ex_status=2, extract_data=data, topic_id=32)
                tube = "extract_info_hg"
                send_msg(base_info, extract_info, tube)
                sended_id_list.append(doc_id)
                sendnum += 1
            if sendnum >= 1:
                break
            if sendnum % 500 == 0:
                print sendnum, "time_cost:", time.time() - begin_time
            if sendnum % 5000 == 0:
                write_file(ff, sended_id_list)

        else:
            print "no result"
    except:
        print traceback.format_exc()
    f.close()
    ff.close()

    print "finish sumnum:%s" % num


import ctypes


def get_md5_i64(obj):
    m = hashlib.md5()
    m.update(obj)
    return ctypes.c_int64(int(m.hexdigest()[8:-8], 16)).value


def send_msg(base_info, extract_info, tube):
    '''往消息队列中发送消息'''
    crawl_info = CrawlInfo()

    pybeanstalk = PyBeanstalk('127.0.0.1')

    req = PageParseInfo(base_info=base_info, crawl_info=crawl_info, extract_info=extract_info, scheduler="a",
                        parse_extends="b", data_extends="c")
    tMemory_b = TMemoryBuffer()
    tBinaryProtocol_b = TBinaryProtocol(tMemory_b)
    req.write(tBinaryProtocol_b)
    body = tMemory_b.getvalue()
    try:
        pybeanstalk.put(tube, body)
        # print "send beanstalk msg success"
    except:
        print traceback.format_exc()


if __name__ == "__main__":
    print "start"
    main()
